import { useAppDataDoneModel } from '@/models/useGlobalModel';
import type { DependencyList, EffectCallback } from 'react';
import { useEffect } from 'react';
import { useRecoilValue } from 'recoil';

/**
 * app加载完成
 */
export default (
  /**
   * 要执行的effect
   */
  effect: EffectCallback,
  /**
   * 需要监听的deps
   */
  deps: DependencyList,
  /**
   * 结束销毁的方法
   */
  unEffect?: () => void,
): void => {
  const appDataDoneModel = useRecoilValue(useAppDataDoneModel);

  useEffect(() => {
    if (appDataDoneModel) {
      effect();
    }

    return () => {
      if (unEffect) {
        unEffect();
      }
    };
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [...deps, appDataDoneModel]);
};
